#include "scene.h"

Scene::Scene()
{
	_big = 0;
	_med = 0;
	_sml = 0;
	_stage_title = "Default stage title";
	_name_1p = "Antrykot[PL]";
	_name_2p = "Firebird[US]";
	life_1p = 5;
	life_2p = 5;
	mousex = 0;
	mousey = 0;
}

Scene::~Scene()
{
	CleanupStage();
}

i32 Scene::DrawStage()
{
	RenderHUD();

	for (u32 c = 0; c < _objects.size(); c++)
    {
        _objects[c]->Render();
    }





	return 1;
}

i32 Scene::SetupDefaultStage()
{
    Graphics *graph = (Graphics*) RES->GET("enem2");

    Enemy *enem = new Enemy;
    enem->px = 100;
    enem->py = 140;
    enem->angle = 2;
    enem->type = 1;
    enem->visual = graph->spr;
    _objects.push_back(enem);

    fx = new Efect;
    fx->Set("born",-1,80,300,100);
    fx->Start();

    _objects.push_back(fx);

    Timer *t = new Timer;
    callbackV1 clb;
    clb.bind(this,&Scene::test);
    t->doPeriodicaly(1000,clb);
    t->start();
    srand(time(0));


    /// players

    Tank *tank_p1, *tank_p2;
    tank_p1 = new Tank;
    tank_p2 = new Tank;
    tank_p1->visual = ((Graphics*)RES->GET("playa1"))->spr;
    tank_p2->visual = ((Graphics*)RES->GET("playa2"))->spr;
    tank_p1->py = g_screen->h - 40;
    tank_p2->py = g_screen->h - 40;
    tank_p1->px = 100;
    tank_p2->px = g_screen->w - 100 - tank_p2->visual.w;
    _objects.push_back(tank_p1);
//    _objects.push_back(tank_p2);
    p1 = tank_p1;
    p2 = tank_p2;
    p1->mousex = mousex;
    p1->mousey = mousey;
    p2->mousex = mousex;
    p2->mousey = mousey;

    return 1;
}

i32  Scene::CleanupStage()
{
	for (u32 c = 0; c < _objects.size(); c++)
    {
        delete _objects[c];
    }
    LOGP1("Scene:Stage clean");
    return 1;
}


i32 Scene::RenderHUD()
{
	SDL_Surface *text;
	SDL_Color cf;
	cf.r = 0xFF;
	cf.g = 0xFF;
	cf.b = 0x00;
	text = TTF_RenderText_Blended ( _sml, _stage_title.c_str(), cf );
	SDL_Rect rct;
	rct.x = 0;
	rct.y = 0;
	rct.w = _screen->w;
	rct.h = text->h + 5;
	SDL_FillRect ( _screen, &rct, 0xFF332288 );
	rct.x = _screen->w / 2 - text->w / 2;
	rct.y = 2;
	rct.w = 9000;
	rct.h = 9000;
	SDL_BlitSurface ( text, 0, _screen, &rct );
	SDL_FreeSurface ( text );

    Graphics *graph = (Graphics*)RES->GET("life");

	for ( i32 c = 0; c < life_1p; c++ )
	{

		rct.y = 2;
		rct.x = 6 + MAX_NAME_PIX + c * ( graph->spr.frames[0].surface[0]->w + 1 );
		SDL_SetAlpha(graph->spr.frames[0].surface[0], SDL_SRCALPHA | SDL_SRCCOLORKEY, 0xFF);
		SDL_BlitSurface ( graph->spr.frames[0].surface[0], 0, _screen, &rct );
	}

	for ( i32 c = 0; c < life_2p; c++ )
	{
		rct.y = 2;
		rct.x = _screen->w - ( 6 + MAX_NAME_PIX + c * ( graph->spr.frames[0].surface[0]->w + 1 ) +
							   graph->spr.frames[0].surface[0]->w );
		SDL_BlitSurface ( graph->spr.frames[0].surface[0], 0, _screen, &rct );
	}

	// p1 name
	cf.r = 34;
	cf.g = 177;
	cf.b = 76;
	text = TTF_RenderText_Blended ( _sml, _name_1p.c_str(), cf );
	rct.x = 2;
	rct.w = 2;
	SDL_Rect r2;
	r2.w = MAX_NAME_PIX;
	r2.h = 1000;
	r2.x = 0;
	r2.y = 0;
	SDL_BlitSurface ( text, &r2, _screen, &rct );
	SDL_FreeSurface ( text );
	// p2 name
	cf.r = 180;
	cf.g = 230;
	cf.b = 29;
	text = TTF_RenderText_Blended ( _sml, _name_2p.c_str(), cf );
	i32 n_len = text->w + 2;

	if ( n_len > MAX_NAME_PIX + 2 ) {
		n_len = MAX_NAME_PIX + 2;
	}

	rct.x = _screen->w - n_len;
	rct.w = 2;
	r2.w = MAX_NAME_PIX;
	r2.h = 1000;
	r2.x = 0;
	r2.y = 0;
	SDL_BlitSurface ( text, &r2, _screen, &rct );
	SDL_FreeSurface ( text );
	return 1;
}


void Scene::test()
{
    fx->setpos(rand() % 600 + 100, rand() % 400 + 100);
    fx->Start();

}

